package com.example.android.adapter;


import android.app.AlertDialog;
import android.graphics.Color;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.android.R;
import com.example.android.bean.CarOrder;
import com.example.android.utils.BaseModel;
import com.example.android.utils.GlideEngine;
import com.example.android.utils.ImageUrlUtil;
import com.example.android.utils.NetUtil;
import com.example.android.utils.api.CarOrderApi;
import com.google.android.material.snackbar.Snackbar;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NewOrderItemAdapter extends BaseQuickAdapter<CarOrder, BaseViewHolder> {
    public NewOrderItemAdapter(List<CarOrder> data) {
        super(R.layout.new_order_list_item, data);

    }


    /**
     * 在此方法中设置item数据
     */
    @Override
    protected void convert(@NotNull BaseViewHolder helper, @NotNull CarOrder item) {
        //收车时间
        TextView time = helper.findView(R.id.inputDateTime);
        time.setText(item.getCloseTime());
        //名称
        TextView userRealName = helper.findView(R.id.name);
        userRealName.setText(item.getCarRelease().getUser().getRealName());
        //车牌
        TextView chepai = helper.findView(R.id.textView19);
        chepai.setText(item.getCarRelease().getCarInfo().getPlaterNumber());
        //车型
        TextView brand = helper.findView(R.id.textView1);
        brand.setText(item.getCarRelease().getCarInfo().getBrand());

        //状态显示
        TextView status = helper.findView(R.id.textView15);
        //地址
        TextView carAddress = helper.findView(R.id.carAddress);
        carAddress.setText(item.getCarRelease().getCarInfo().getAddress());

        //收车按钮按钮
        Button button = helper.findView(R.id.yuyue);

        CircleImageView profileImage = helper.findView(R.id.profile_image);
        //主图
        String imagesUrl = ImageUrlUtil.getImagesUrl(item.getCarRelease().getCarInfo());
        if (imagesUrl != null) {
            GlideEngine.createGlideEngine().loadGridImage(getContext(), imagesUrl, profileImage);
        }
        //使用时间
        TextView userDateTime = helper.findView(R.id.userDateTime);
        userDateTime.setText(item.getUseTime());

        //开始时间
        TextView start = helper.findView(R.id.textView11);
        start.setText(item.getCarRelease().getStartTime());
        //结束时间
        TextView end = helper.findView(R.id.textView13);
        end.setText(item.getCarRelease().getFinishTimew());
        //判断状态数据状态
        //频道按钮状态数据
        switch (item.getStatus()) {
            case 1:
                setText(button, "已取车", false);
                //在取消按钮中进行绑定事件
                button.setOnClickListener(view -> {
                    //创建输入框
                    EditText editText = new EditText(getContext());
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                    //设置弹出框
                    new AlertDialog.Builder(getContext())
                            .setTitle("请输入安全码！")
                            .setView(editText)
                            .setPositiveButton("取消", null).setPositiveButton("确定", (dialogInterface, i) -> NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(CarOrderApi.class)
                                    .verifyCode(item.getId(), Integer.valueOf(editText.getText().toString().equals("")?"1":editText.getText().toString()))
                                    .enqueue(new Callback<BaseModel<String>>() {
                                        @Override
                                        public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
                                            if (response.body() != null && response.body().getCode() == 200) {
                                                Snackbar.make(view, "成功：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                                            } else {
                                                Snackbar.make(view, "错误：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<BaseModel<String>> call, Throwable t) {
                                            Snackbar.make(view, "失败：" + t.getMessage(), Snackbar.LENGTH_LONG).show();

                                        }
                                    })
                            ).show();
                });
                break;
            case 2:
                setText(button, "已还车", false);
                button.setOnClickListener(view -> new AlertDialog.Builder(getContext()).setTitle("客户是否已还车？")
                        .setPositiveButton("取消", null)
                        .setPositiveButton("确定", (dialogInterface, i) -> NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(CarOrderApi.class).getUserCar(item.getId()).enqueue(new Callback<BaseModel<String>>() {
                            @Override
                            public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
                                if (response.body() != null && response.body().getCode() == 200) {
                                    Snackbar.make(view, "还车成功：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                                } else {
                                    Snackbar.make(view, "还车失败：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                                }
                            }

                            @Override
                            public void onFailure(Call<BaseModel<String>> call, Throwable t) {
                                Snackbar.make(view, "还车失败：" + t.getMessage(), Snackbar.LENGTH_LONG).show();

                            }
                        }))

                        .show()
                );
                break;
            case 3:
                setText(button, "已还车", true);
                break;
            case 4:
                setText(button, "已取消", true);
                break;
        }

    }

    private void setText(Button button, String text, Boolean isBlackColor) {
        if (isBlackColor) {
            button.setTextColor(Color.BLACK);
            button.getBackground().setAlpha(255);
        }
        button.setText(text);
    }

    private void setText(TextView textView, String text, Boolean isBlackColor) {
        if (isBlackColor) {
            textView.setVisibility(View.VISIBLE);
        }
        textView.setText(text);
    }
}
